/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cadastros;

import dao.DaoFuncionario;
import modelo.Cidade;
import modelo.Funcionario;
import modelo.Servico;
import util.pesquisarCidade;

/**
 *
 * @author fumio
 */
public class CadFuncionario extends javax.swing.JDialog {
    private DaoFuncionario daofuncionario = new DaoFuncionario();
    private Cidade cidade;
    private Servico servico;
    /**
     * Creates new form CadFuncionario
     */
    public CadFuncionario(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        btnGravar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        pnlFuncionario = new javax.swing.JTabbedPane();
        pnlCadastro = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        edtObs = new javax.swing.JTextArea();
        edtTelefone = new javax.swing.JTextField();
        edtEndereco = new javax.swing.JTextField();
        edtNome = new javax.swing.JTextField();
        edtCodigo = new javax.swing.JTextField();
        edtNo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        edtCelular = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        btnPesquisar = new javax.swing.JButton();
        edtCidade = new javax.swing.JTextField();
        edtCodCidade = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        edtUF = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        edtCodServico = new javax.swing.JTextField();
        btnPesquisar1 = new javax.swing.JButton();
        edtServico = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        edtLogin = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        edtSenha = new javax.swing.JPasswordField();
        pnlPermissao = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnGravar.setText("Salvar");
        btnGravar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGravarActionPerformed(evt);
            }
        });
        jPanel2.add(btnGravar, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 5, 70, 50));

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        jPanel2.add(btnCancelar, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 5, -1, 50));

        jPanel1.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 450, 520, 70));

        pnlFuncionario.setToolTipText("Cadastro");

        pnlCadastro.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        pnlCadastro.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setText("Obs:");
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        pnlCadastro.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 310, -1, -1));

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel2.setText("Código:");
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        pnlCadastro.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 20, -1, -1));

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel7.setText("Nome:");
        jLabel7.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        pnlCadastro.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 60, -1, -1));

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText("Endereco:");
        jLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        pnlCadastro.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 110, -1, -1));

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel6.setText("Telefone:");
        jLabel6.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        pnlCadastro.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 190, -1, -1));

        edtObs.setColumns(20);
        edtObs.setRows(5);
        jScrollPane1.setViewportView(edtObs);

        pnlCadastro.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 300, 350, -1));
        pnlCadastro.add(edtTelefone, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 180, 140, -1));
        pnlCadastro.add(edtEndereco, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 100, 270, -1));
        pnlCadastro.add(edtNome, new org.netbeans.lib.awtextra.AbsoluteConstraints(132, 50, 350, -1));

        edtCodigo.setEnabled(false);
        pnlCadastro.add(edtCodigo, new org.netbeans.lib.awtextra.AbsoluteConstraints(132, 13, 60, -1));
        pnlCadastro.add(edtNo, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 100, 50, -1));

        jLabel5.setText("Nº");
        pnlCadastro.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 110, -1, -1));
        pnlCadastro.add(edtCelular, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 180, 140, -1));

        jLabel10.setText("Celular:");
        pnlCadastro.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 190, -1, -1));

        btnPesquisar.setText("P");
        btnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarActionPerformed(evt);
            }
        });
        pnlCadastro.add(btnPesquisar, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 140, 50, -1));
        pnlCadastro.add(edtCidade, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 140, 170, -1));
        pnlCadastro.add(edtCodCidade, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 140, 50, -1));

        jLabel12.setText("Cidade:");
        pnlCadastro.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 140, -1, 30));
        pnlCadastro.add(edtUF, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 140, 50, -1));

        jLabel11.setText("UF:");
        pnlCadastro.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 150, -1, -1));

        jLabel13.setText("Serviço:");
        pnlCadastro.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 220, -1, 30));
        pnlCadastro.add(edtCodServico, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 220, 50, -1));

        btnPesquisar1.setText("P");
        btnPesquisar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisar1ActionPerformed(evt);
            }
        });
        pnlCadastro.add(btnPesquisar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 220, 50, -1));
        pnlCadastro.add(edtServico, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 220, 250, -1));

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel8.setText("Login:");
        jLabel8.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        pnlCadastro.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 270, -1, -1));
        pnlCadastro.add(edtLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 260, 140, -1));

        jLabel4.setText("Senha:");
        pnlCadastro.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 260, -1, -1));

        edtSenha.setText("jPasswordField1");
        pnlCadastro.add(edtSenha, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 260, 130, -1));

        pnlFuncionario.addTab("Cadastro", pnlCadastro);

        javax.swing.GroupLayout pnlPermissaoLayout = new javax.swing.GroupLayout(pnlPermissao);
        pnlPermissao.setLayout(pnlPermissaoLayout);
        pnlPermissaoLayout.setHorizontalGroup(
            pnlPermissaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 512, Short.MAX_VALUE)
        );
        pnlPermissaoLayout.setVerticalGroup(
            pnlPermissaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 413, Short.MAX_VALUE)
        );

        pnlFuncionario.addTab("Permissão", pnlPermissao);

        jPanel1.add(pnlFuncionario, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 520, 450));
        pnlFuncionario.getAccessibleContext().setAccessibleName("Cadastro");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-530)/2, (screenSize.height-550)/2, 530, 550);
    }// </editor-fold>//GEN-END:initComponents

    private void btnGravarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGravarActionPerformed
        // TODO add your handling code here:
        if (validar()){
        daofuncionario.salvar(Long.parseLong(edtCodigo.getText()),
                edtNome.getText(), 
                edtEndereco.getText(),
                edtNo.getText(),
                cidade, 
                edtTelefone.getText(),
                edtObs.getText(), 
                edtCelular.getText(), 
          //      null,
                edtLogin.getText(), 
                edtSenha.getText());
            
        }
        
        
    }//GEN-LAST:event_btnGravarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarActionPerformed
        // TODO add your handling code here:
        pesquisarCidade pesquisa = new pesquisarCidade(null, rootPaneCheckingEnabled);
        cidade = pesquisa.Seleciona();
        edtCodCidade.setText(cidade.getCod_cidade().toString());
        edtCidade.setText(cidade.getCidade().toString());
        edtUF.setText(cidade.getUf());
        
    }//GEN-LAST:event_btnPesquisarActionPerformed

    private void btnPesquisar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisar1ActionPerformed
        // TODO add your handling code here:
      /*  pesquisaServico pesquisar = new pesquisaServico(null, rootPaneCheckingEnabled);
        servico = pesquisar.Seleciona();
        edtCodServico.setText(); */
    }//GEN-LAST:event_btnPesquisar1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CadFuncionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CadFuncionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CadFuncionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CadFuncionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                CadFuncionario dialog = new CadFuncionario(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGravar;
    private javax.swing.JButton btnPesquisar;
    private javax.swing.JButton btnPesquisar1;
    private javax.swing.JTextField edtCelular;
    private javax.swing.JTextField edtCidade;
    private javax.swing.JTextField edtCodCidade;
    private javax.swing.JTextField edtCodServico;
    private javax.swing.JTextField edtCodigo;
    private javax.swing.JTextField edtEndereco;
    private javax.swing.JTextField edtLogin;
    private javax.swing.JTextField edtNo;
    private javax.swing.JTextField edtNome;
    private javax.swing.JTextArea edtObs;
    private javax.swing.JPasswordField edtSenha;
    private javax.swing.JTextField edtServico;
    private javax.swing.JTextField edtTelefone;
    private javax.swing.JTextField edtUF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnlCadastro;
    private javax.swing.JTabbedPane pnlFuncionario;
    private javax.swing.JPanel pnlPermissao;
    // End of variables declaration//GEN-END:variables
    private boolean validar(){
        if(edtNome.getText().equals("")){
            edtNome.requestFocusInWindow();
            return false;
        }else if(edtCelular.getText().equals("")){
            edtCelular.requestFocusInWindow();
            return false;
        }else if (edtTelefone.getText().equals("")){
            edtTelefone.requestFocusInWindow();
            return false;
        }
        if (edtCodigo.getText().equals("")){
            edtCodigo.setText("0");
        }
        return true;
    }

    public void alterar(Object obj){
        Funcionario funcionario = (Funcionario) obj;
        edtCodigo.setText(funcionario.getCod_pessoa().toString());
        edtCelular.setText(funcionario.getCelular().toString());
        edtCidade.setText(funcionario.getCidade().getCidade().toString());
        edtCodCidade.setText(funcionario.getCidade().getCod_cidade().toString());
    //    edtCodServico.setText(funcionario.getServico().getCod_servico().toString());
        edtEndereco.setText(funcionario.getEndereco().toString());
        edtLogin.setText(funcionario.getLogin().toString());
        edtNo.setText(funcionario.getNumero().toString());
        edtNome.setText(funcionario.getNome().toString());
        edtObs.setText(funcionario.getObs().toString());
     //   edtServico.setText(funcionario.getServico().getServico().toString());
        edtTelefone.setText(funcionario.getTelefone().toString());
        edtUF.setText(funcionario.getCidade().getUf());
                
    }
    

}
